package com.ruoyi.classai.service.impl;


import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.ruoyi.classai.domain.*;
import com.ruoyi.classai.domain.dto.px.PxCourseQuestionnaireDto;
import com.ruoyi.classai.domain.dto.px.PxTrainingCourseDto;
import com.ruoyi.classai.domain.dto.px.PxTrainingCourseHomeworkDto;
import com.ruoyi.classai.domain.dto.px.PxTrainingCourseVideoDto;
import com.ruoyi.classai.domain.vo.px.PxCourseQuestionnaireVo;
import com.ruoyi.classai.domain.vo.px.PxCourseVideoVo;
import com.ruoyi.classai.domain.vo.px.PxTrainingCourseVo;
import com.ruoyi.classai.mapper.PxTrainingCourseMapper;
import com.ruoyi.classai.service.*;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.context.ThirdSessionHolder;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * 培训课程Service业务层处理
 * 
 * @author lcj
 * @date 2025-06-18
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PxTrainingCourseServiceImpl extends MPJBaseServiceImpl<PxTrainingCourseMapper, PxTrainingCourse> implements IPxTrainingCourseService {

    @Lazy
    @Autowired
    private IPxTrainingTeacherService trainingTeacherService;
    @Lazy
    @Autowired
    private IPxTrainingCourseFileService trainingCourseFileService;
    @Lazy
    @Autowired
    private IPxTrainingCourseQuestionnaireService trainingCourseQuestionnaireService;
    @Lazy
    @Autowired
    private IPxTrainingCourseCompleteService trainingCourseCompleteService;
    @Lazy
    @Autowired
    private IPxTrainingCourseQuestionnaireSubmitService trainingCourseQuestionnaireSubmitService;
    @Lazy
    @Autowired
    private IPxTrainingCourseVideoService trainingCourseVideoService;
    @Lazy
    @Autowired
    private IPxTrainingCourseHomeworkService trainingCourseHomeworkService;
    @Lazy
    @Autowired
    private IPxTrainingCourseSigninService trainingCourseSigninService;
    @Lazy
    @Autowired
    private IPxTrainingService trainingService;
    @Lazy
    @Autowired
    private IPxTrainingMessageService trainingMessageService;

    @Override
    public void addTrainingCourse(PxTrainingCourseDto pxTrainingCourse) {
        PxTrainingCourse trainingCourse=new PxTrainingCourse();
        BeanUtils.copyProperties(pxTrainingCourse,trainingCourse);
        //添加培训
        this.save(trainingCourse);
        int sort=0;
        //添加问卷调查
        List<PxTrainingCourseQuestionnaire> courseQuestionnaires= pxTrainingCourse.getTrainingCourseQuestionnaireList();
        for (PxTrainingCourseQuestionnaire d : courseQuestionnaires) {
           d.setTrainingCourseId(trainingCourse.getId());
           d.setSort(sort++);
           trainingCourseQuestionnaireService.save(d);
        }
        //添加线上课程
        pxTrainingCourse.getTrainingCourseFileList().forEach(d->{
            d.setTrainingCourseId(trainingCourse.getId());
            trainingCourseFileService.save(d);
        });

        //todo 可能存在事务问题
        /*更新培训课时*/
        int time=0;
        List<PxTrainingCourse> list=this.list(Wrappers.<PxTrainingCourse>lambdaQuery().eq(PxTrainingCourse::getTrainingId,pxTrainingCourse.getTrainingId())
                 .eq(PxTrainingCourse::getStatus,Constants.COMMON_SUCCESS));
        for (PxTrainingCourse course : list) {
            if (Objects.nonNull(course.getClassHour())) {
                time = course.getClassHour() + time;
            }
        }
        //更新时间
        if (time>0){
            PxTraining pxTraining=new PxTraining();
            pxTraining.setId(pxTrainingCourse.getTrainingId());
            pxTraining.setTrainingHours(time);
            trainingService.updateById(pxTraining);
        }
    }

    @Override
    public void editTrainingCourse(PxTrainingCourseDto pxTrainingCourse) {
        PxTrainingCourse trainingCourse=new PxTrainingCourse();
        BeanUtils.copyProperties(pxTrainingCourse,trainingCourse);
        //添加培训
        this.updateById(trainingCourse);
        //删除问卷调查
        trainingCourseQuestionnaireService.remove(Wrappers.<PxTrainingCourseQuestionnaire>lambdaQuery().eq(PxTrainingCourseQuestionnaire::getTrainingCourseId,pxTrainingCourse.getId()));
        //删除线上课程
        //trainingCourseFileService.remove(Wrappers.<PxTrainingCourseFile>lambdaQuery().eq(PxTrainingCourseFile::getTrainingCourseId,pxTrainingCourse.getId()));
        //添加问卷调查
        int sort=0;
        List<PxTrainingCourseQuestionnaire> courseQuestionnaires= pxTrainingCourse.getTrainingCourseQuestionnaireList();
        for (PxTrainingCourseQuestionnaire d : courseQuestionnaires) {
            d.setTrainingCourseId(trainingCourse.getId());
            d.setSort(sort++);
            trainingCourseQuestionnaireService.save(d);
        }
        //添加线上课程
        pxTrainingCourse.getTrainingCourseFileList().forEach(d->{
            if (StringUtils.isNotEmpty(d.getId())) {
                d.setTrainingCourseId(trainingCourse.getId());
                trainingCourseFileService.updateById(d);
            }else {
                d.setTrainingCourseId(trainingCourse.getId());
                trainingCourseFileService.save(d);
            }
        });

        //todo 可能存在事务问题
        /*更新培训课时*/
        int time=0;
        List<PxTrainingCourse> list=this.list(Wrappers.<PxTrainingCourse>lambdaQuery().eq(PxTrainingCourse::getTrainingId,pxTrainingCourse.getTrainingId())
                .eq(PxTrainingCourse::getStatus,Constants.COMMON_SUCCESS));
        for (PxTrainingCourse course : list) {
            if (Objects.nonNull(course.getClassHour())) {
                time = course.getClassHour() + time;
            }
        }
        //更新时间
        if (time>0){
            PxTraining pxTraining=new PxTraining();
            pxTraining.setId(pxTrainingCourse.getTrainingId());
            pxTraining.setTrainingHours(time);
            trainingService.updateById(pxTraining);
        }
    }

    @Override
    public PxTrainingCourseVo getTrainingCourseById(String id,int type) {
        PxTrainingCourse trainingCourse=this.getById(id);
        if (Objects.isNull(trainingCourse)){
            throw new GlobalException("没有查询到课程信息");
        }
        PxTrainingCourseVo pxTrainingCourseVo=new PxTrainingCourseVo();
        BeanUtils.copyProperties(trainingCourse,pxTrainingCourseVo);
        if (type==0) {
            //查询问卷调查
            pxTrainingCourseVo.setTrainingCourseQuestionnaireList(trainingCourseQuestionnaireService.list(Wrappers.<PxTrainingCourseQuestionnaire>lambdaQuery().eq(PxTrainingCourseQuestionnaire::getTrainingCourseId, id)));
            //查询线上课程
            pxTrainingCourseVo.setTrainingCourseFileList(trainingCourseFileService.list(Wrappers.<PxTrainingCourseFile>lambdaQuery().eq(PxTrainingCourseFile::getTrainingCourseId, id)));

        }
        return pxTrainingCourseVo;
    }

    @Override
    public PxTrainingCourseComplete getTrainingCourseStatus(String teacherId,String id) {
        return trainingCourseCompleteService.getOne(Wrappers.<PxTrainingCourseComplete>lambdaQuery().eq(PxTrainingCourseComplete::getTrainingCourseId,id)
                .eq(PxTrainingCourseComplete::getTeacherId,teacherId)
                .select(PxTrainingCourseComplete::getIsVideo,PxTrainingCourseComplete::getIsSubWork,
                        PxTrainingCourseComplete::getIsSubQuestionnaire,PxTrainingCourseComplete::getIsSignin));
    }

    @Override
    public List<PxTrainingCourse> getTrainingCourseList(String trainingId,String teacherId) {
        List<PxTrainingCourse> list=this.list(Wrappers.<PxTrainingCourse>lambdaQuery().eq(PxTrainingCourse::getTrainingId,trainingId)
                .eq(PxTrainingCourse::getStatus, Constants.COMMON_SUCCESS)
                .orderByAsc(PxTrainingCourse::getStartTime,PxTrainingCourse::getEndTime));
        list.forEach(data->{
            //判断课程的开始时间是否小于当前时间
            if(data.getStartTime().before(DateUtil.date())){
                //查询课程是否完成
                PxTrainingCourseComplete trainingCourseComplete=trainingCourseCompleteService.getOne(Wrappers.<PxTrainingCourseComplete>lambdaQuery().eq(PxTrainingCourseComplete::getTrainingCourseId,data.getId())
                        .eq(PxTrainingCourseComplete::getTeacherId,teacherId));
                if (Objects.nonNull(trainingCourseComplete)&&trainingCourseComplete.getStatus()==1){
                    //已完成
                    data.setStatus(1);
                }else {
                    //未完成
                    data.setStatus(2);
                }
            }else {
                //未开始
                data.setStatus(0);
            }
        });
        return list;
    }

    @Override
    public List<PxCourseVideoVo> getCourseVideoList(String trainingCourseId, String teacherId) {
        List<PxCourseVideoVo> courseVideoVos=new ArrayList<>();
        //查询课程视频
        List<PxTrainingCourseFile> list=trainingCourseFileService.list(Wrappers.<PxTrainingCourseFile>lambdaQuery().eq(PxTrainingCourseFile::getType,0)
                .eq(PxTrainingCourseFile::getTrainingCourseId,trainingCourseId).orderByAsc(PxTrainingCourseFile::getCreateTime));
        list.forEach(data->{
            PxCourseVideoVo courseVideoVo=new PxCourseVideoVo();
            BeanUtils.copyProperties(data,courseVideoVo);
            //查询课程视频进度
            PxTrainingCourseVideo courseVideo=trainingCourseVideoService.getOne(Wrappers.<PxTrainingCourseVideo>lambdaQuery().eq(PxTrainingCourseVideo::getVideoId,data.getId())
                    .eq(PxTrainingCourseVideo::getTeacherId,teacherId).eq(PxTrainingCourseVideo::getTrainingCourseId,trainingCourseId));
            if (Objects.nonNull(courseVideo)){
                courseVideoVo.setVideoProgress(courseVideo.getVideoProgress());
                courseVideoVo.setVideoDuration(courseVideo.getVideoDuration());
            }
            courseVideoVos.add(courseVideoVo);
        });
        return courseVideoVos;
    }

    @Override
    public List<PxTrainingCourseFile> getCourseDocumentList(String trainingCourseId) {
        return trainingCourseFileService.list(Wrappers.<PxTrainingCourseFile>lambdaQuery().eq(PxTrainingCourseFile::getType,1)
                .eq(PxTrainingCourseFile::getTrainingCourseId,trainingCourseId).orderByAsc(PxTrainingCourseFile::getCreateTime));
    }

    @Override
    public List<PxTrainingCourseHomework> getHomeworkList(String trainingCourseId, String teacherId) {
        return trainingCourseHomeworkService.list(Wrappers.<PxTrainingCourseHomework>lambdaQuery().eq(PxTrainingCourseHomework::getTrainingCourseId,trainingCourseId)
                .eq(PxTrainingCourseHomework::getTeacherId,teacherId).eq(PxTrainingCourseHomework::getStatus,Constants.COMMON_SUCCESS).orderByDesc(PxTrainingCourseHomework::getCreateTime));
    }

    @Override
    public List<PxCourseQuestionnaireVo> getQuestionnaireList(String trainingCourseId, String teacherId) {
        List<PxCourseQuestionnaireVo> questionnaireVos=new ArrayList<>();
        //查询问卷信息
        List<PxTrainingCourseQuestionnaire> list=trainingCourseQuestionnaireService.list(Wrappers.<PxTrainingCourseQuestionnaire>lambdaQuery().eq(PxTrainingCourseQuestionnaire::getTrainingCourseId,trainingCourseId)
                .orderByAsc(PxTrainingCourseQuestionnaire::getSort));
        list.forEach(data->{
            PxCourseQuestionnaireVo questionnaireVo=new PxCourseQuestionnaireVo();
            BeanUtils.copyProperties(data,questionnaireVo);
            //查询是否评价信息
            PxTrainingCourseQuestionnaireSubmit questionnaireSubmit=trainingCourseQuestionnaireSubmitService.getOne(Wrappers.<PxTrainingCourseQuestionnaireSubmit>lambdaQuery().eq(PxTrainingCourseQuestionnaireSubmit::getQuestionnaireId,data.getId())
                    .eq(PxTrainingCourseQuestionnaireSubmit::getTrainingCourseId,trainingCourseId).eq(PxTrainingCourseQuestionnaireSubmit::getTeacherId,teacherId));
            if (Objects.nonNull(questionnaireSubmit)){
                questionnaireVo.setQuestionnaireContent(questionnaireSubmit.getQuestionnaireContent());
            }
            questionnaireVos.add(questionnaireVo);
        });
        return questionnaireVos;
    }

    @Override
    public void scanCodeSignin(String trainingCourseId, String teacherId) {
        //查询课程信息
        PxTrainingCourse pxTrainingCourse=this.getOne(Wrappers.<PxTrainingCourse>lambdaQuery().eq(PxTrainingCourse::getId,trainingCourseId)
                .eq(PxTrainingCourse::getStatus,Constants.COMMON_SUCCESS));
        if (Objects.isNull(pxTrainingCourse)){
            throw new GlobalException("没有查询到课程信息,签到失败");
        }
        //查询是否已签到
        PxTrainingCourseSignin trainingCourseSignin=trainingCourseSigninService.getOne(Wrappers.<PxTrainingCourseSignin>lambdaQuery()
                .eq(PxTrainingCourseSignin::getTrainingCourseId,trainingCourseId)
                .eq(PxTrainingCourseSignin::getTeacherId,teacherId));
        if (Objects.isNull(trainingCourseSignin)){
            trainingCourseSignin=new PxTrainingCourseSignin();
            trainingCourseSignin.setTrainingCourseId(trainingCourseId);
            trainingCourseSignin.setTeacherId(teacherId);
            trainingCourseSigninService.save(trainingCourseSignin);
            //异步执行 签到后操作
            CompletableFuture.runAsync(() -> {
                PxTrainingCourseComplete courseComplete=trainingCourseCompleteService.getOne(Wrappers.<PxTrainingCourseComplete>lambdaQuery()
                        .eq(PxTrainingCourseComplete::getTrainingCourseId,trainingCourseId)
                        .eq(PxTrainingCourseComplete::getTeacherId,teacherId));
                if (Objects.nonNull(courseComplete)) {
                    courseComplete.setId(courseComplete.getId());
                }else {
                    courseComplete = new PxTrainingCourseComplete();
                }
                boolean isScore=false;
                //查询是否有课后作业和问卷
                if (pxTrainingCourse.getIsWork()==0&&pxTrainingCourse.getIsQuestionnaire()==0){
                    //完成课程
                    courseComplete.setStatus(Constants.COMMON_SUCCESS);
                    //todo 执行加分
                    isScore=true;
                }else {
                    //未完成
                    courseComplete.setStatus(Constants.COMMON_DELETE);
                }
                courseComplete.setTrainingCourseId(trainingCourseId);
                courseComplete.setTrainingId(pxTrainingCourse.getTrainingId());
                courseComplete.setTeacherId(teacherId);
                courseComplete.setTeacherScore(pxTrainingCourse.getClassHour());
                //设置已签到
                courseComplete.setIsSignin(Constants.COMMON_SUCCESS);
                trainingCourseCompleteService.saveOrUpdate(courseComplete);
                //执行加分
                if (isScore){
                    trainingTeacherService.addScore(courseComplete.getTrainingId(),courseComplete.getTeacherId(),courseComplete.getTeacherScore());
                }
            });
        }

    }

    @Override
    public void addHomework(PxTrainingCourseHomeworkDto trainingCourseHomework) {

        PxTrainingCourseHomework courseHomework=new PxTrainingCourseHomework();
        BeanUtils.copyProperties(trainingCourseHomework,courseHomework);
        courseHomework.setStatus(1);
        trainingCourseHomeworkService.save(courseHomework);
        //异步执行 签到后操作
        CompletableFuture.runAsync(() -> {
            //查询课程信息
            PxTrainingCourse pxTrainingCourse=this.getById(trainingCourseHomework.getTrainingCourseId());
            PxTrainingCourseComplete courseComplete=trainingCourseCompleteService.getOne(Wrappers.<PxTrainingCourseComplete>lambdaQuery()
                    .eq(PxTrainingCourseComplete::getTrainingCourseId,trainingCourseHomework.getTrainingCourseId())
                    .eq(PxTrainingCourseComplete::getTeacherId,trainingCourseHomework.getTeacherId()));
            if (Objects.isNull(courseComplete)) { //课程完成信息新建，提交作业
                courseComplete= new PxTrainingCourseComplete();
                //设置未完成
                courseComplete.setStatus(Constants.COMMON_DELETE);
                courseComplete.setTrainingCourseId(trainingCourseHomework.getTrainingCourseId());
                courseComplete.setTrainingId(pxTrainingCourse.getTrainingId());
                courseComplete.setTeacherId(trainingCourseHomework.getTeacherId());
                //学分
                courseComplete.setTeacherScore(pxTrainingCourse.getClassHour());
                //已提交课程
                courseComplete.setIsSubWork(Constants.COMMON_SUCCESS);
                trainingCourseCompleteService.save(courseComplete);
            }else {
                if(courseComplete.getStatus()==Constants.COMMON_DELETE) { //判断课程是否完成
                    boolean isScore=false;
                    //判断是否签到并上传问卷
                    if (courseComplete.getIsSignin() == Constants.COMMON_SUCCESS) {
                        //判断问卷调查是否完成或者没有问卷调查
                        if(courseComplete.getIsSubQuestionnaire()==Constants.COMMON_SUCCESS||pxTrainingCourse.getIsQuestionnaire()==Constants.COMMON_DELETE) {
                            //课程完成
                            courseComplete.setStatus(Constants.COMMON_SUCCESS);
                            //todo 加分
                            isScore=true;
                        }

                    }else{ //未签到
                        //判断线上课程是否完成
                        if (courseComplete.getIsVideo()==Constants.COMMON_SUCCESS){
                            //判断问卷调查是否完成或者没有问卷调查
                            if(courseComplete.getIsSubQuestionnaire()==Constants.COMMON_SUCCESS||pxTrainingCourse.getIsQuestionnaire()==Constants.COMMON_DELETE) {
                                //课程完成
                                courseComplete.setStatus(Constants.COMMON_SUCCESS);
                                //todo 加分
                                isScore=true;
                            }
                        }
                    }
                    //已提交课程
                    courseComplete.setIsSubWork(Constants.COMMON_SUCCESS);
                    trainingCourseCompleteService.updateById(courseComplete);
                    //执行加分
                    if (isScore){
                        trainingTeacherService.addScore(courseComplete.getTrainingId(),courseComplete.getTeacherId(),courseComplete.getTeacherScore());
                    }
                }
            }
        });
    }

    @Override
    public void addQuestionnaire(List<PxCourseQuestionnaireDto> questionnaires) {
        questionnaires.forEach(data->{
            //删除问卷信息
            trainingCourseQuestionnaireSubmitService.remove(Wrappers.<PxTrainingCourseQuestionnaireSubmit>lambdaQuery()
                    .eq(PxTrainingCourseQuestionnaireSubmit::getQuestionnaireId,data.getQuestionnaireId())
                    .eq(PxTrainingCourseQuestionnaireSubmit::getTrainingCourseId,data.getTrainingCourseId())
                    .eq(PxTrainingCourseQuestionnaireSubmit::getTeacherId,data.getTeacherId()));
            //添加问卷
            PxTrainingCourseQuestionnaireSubmit questionnaireSubmit=new PxTrainingCourseQuestionnaireSubmit();
            BeanUtils.copyProperties(data,questionnaireSubmit);
            trainingCourseQuestionnaireSubmitService.save(questionnaireSubmit);
        });
        //异步执行提交问卷后操作
        CompletableFuture.runAsync(() -> {
            //查询课程信息
            PxTrainingCourse pxTrainingCourse=this.getById(questionnaires.get(0).getTrainingCourseId());
            PxTrainingCourseComplete courseComplete=trainingCourseCompleteService.getOne(Wrappers.<PxTrainingCourseComplete>lambdaQuery()
                    .eq(PxTrainingCourseComplete::getTrainingCourseId,questionnaires.get(0).getTrainingCourseId())
                    .eq(PxTrainingCourseComplete::getTeacherId,questionnaires.get(0).getTeacherId()));
            if (Objects.isNull(courseComplete)) { //课程完成信息新建，提交问卷
                courseComplete= new PxTrainingCourseComplete();
                //设置未完成
                courseComplete.setStatus(Constants.COMMON_DELETE);
                courseComplete.setTrainingCourseId(questionnaires.get(0).getTrainingCourseId());
                courseComplete.setTrainingId(pxTrainingCourse.getTrainingId());
                courseComplete.setTeacherId(questionnaires.get(0).getTeacherId());
                //学分
                courseComplete.setTeacherScore(pxTrainingCourse.getClassHour());
                //已提交问卷课程
                courseComplete.setIsSubQuestionnaire(Constants.COMMON_SUCCESS);
                trainingCourseCompleteService.save(courseComplete);
            }else {
                if(courseComplete.getStatus()==Constants.COMMON_DELETE) { //判断课程是否完成
                    boolean isScore=false;
                    //判断是否签到并上传问卷
                    if (courseComplete.getIsSignin() == Constants.COMMON_SUCCESS) {
                        //判断问卷调查是否完成或者没有问卷调查
                        if(courseComplete.getIsSubWork()==Constants.COMMON_SUCCESS||pxTrainingCourse.getIsWork()==Constants.COMMON_DELETE) {
                            //课程完成
                            courseComplete.setStatus(Constants.COMMON_SUCCESS);
                            //todo 加分
                            isScore=true;
                        }

                    }else{ //未签到
                        //判断线上课程是否完成
                        if (courseComplete.getIsVideo()==Constants.COMMON_SUCCESS){
                            //判断问卷调查是否完成或者没有问卷调查
                            if(courseComplete.getIsSubWork()==Constants.COMMON_SUCCESS||pxTrainingCourse.getIsWork()==Constants.COMMON_DELETE) {
                                //课程完成
                                courseComplete.setStatus(Constants.COMMON_SUCCESS);
                                //todo 加分
                                isScore=true;
                            }
                        }
                    }
                    //已提交课程
                    courseComplete.setIsSubQuestionnaire(Constants.COMMON_SUCCESS);
                    trainingCourseCompleteService.updateById(courseComplete);
                    //执行加分
                    if (isScore){
                        trainingTeacherService.addScore(courseComplete.getTrainingId(),courseComplete.getTeacherId(),courseComplete.getTeacherScore());
                    }
                }
            }
        });
    }

    @Override
    public void addCourseVideo(PxTrainingCourseVideoDto courseVideo) {
        //查询课程视频记录
        PxTrainingCourseVideo trainingCourseVideo=trainingCourseVideoService.getOne(Wrappers.<PxTrainingCourseVideo>lambdaQuery()
                .eq(PxTrainingCourseVideo::getTrainingCourseId,courseVideo.getTrainingCourseId())
                .eq(PxTrainingCourseVideo::getVideoId,courseVideo.getVideoId())
                .eq(PxTrainingCourseVideo::getTeacherId,courseVideo.getTeacherId()));
        if (Objects.isNull(trainingCourseVideo)){
            trainingCourseVideo=new PxTrainingCourseVideo();
            BeanUtils.copyProperties(courseVideo,trainingCourseVideo);
            trainingCourseVideoService.save(trainingCourseVideo);
        }else {
            trainingCourseVideo.setVideoProgress(courseVideo.getVideoProgress());
            if (Objects.nonNull(courseVideo.getStatus())&&courseVideo.getStatus()==1) {
                trainingCourseVideo.setStatus(1);
            }
            trainingCourseVideo.setVideoDuration(courseVideo.getVideoDuration());
            trainingCourseVideoService.updateById(trainingCourseVideo);
        }
        //判断当前视频是否完成
        if (trainingCourseVideo.getStatus()==Constants.COMMON_SUCCESS) {
            //异步执行提交问卷后操作
            CompletableFuture.runAsync(() -> {
                //查询课程信息
                PxTrainingCourse pxTrainingCourse = this.getById(courseVideo.getTrainingCourseId());
                /*统计所有视频是否完成*/
                //已完成视频教学
                boolean isVideo=true;
                //查询课程下的在线视频
                List<PxTrainingCourseFile> courseFiles=trainingCourseFileService.list(Wrappers.<PxTrainingCourseFile>lambdaQuery().eq(PxTrainingCourseFile::getType,0)
                        .eq(PxTrainingCourseFile::getTrainingCourseId,courseVideo.getTrainingCourseId()));
                for (PxTrainingCourseFile courseFile : courseFiles) {
                    //查询视频记录
                    PxTrainingCourseVideo trainingCourseVideo1=trainingCourseVideoService.getOne(Wrappers.<PxTrainingCourseVideo>lambdaQuery()
                            .eq(PxTrainingCourseVideo::getTrainingCourseId,courseVideo.getTrainingCourseId())
                            .eq(PxTrainingCourseVideo::getVideoId,courseFile.getId())
                            .eq(PxTrainingCourseVideo::getTeacherId,courseVideo.getTeacherId()));
                    //没有观看视频
                    if (Objects.isNull(trainingCourseVideo1)){
                        isVideo=false;
                        break;
                    }else {
                        //视频未完成
                        if (trainingCourseVideo1.getStatus()==Constants.COMMON_DELETE){
                            isVideo=false;
                            break;
                        }
                    }
                };

                if (isVideo) { //已完成视频教学
                    PxTrainingCourseComplete courseComplete = trainingCourseCompleteService.getOne(Wrappers.<PxTrainingCourseComplete>lambdaQuery()
                            .eq(PxTrainingCourseComplete::getTrainingCourseId, courseVideo.getTrainingCourseId())
                            .eq(PxTrainingCourseComplete::getTeacherId, courseVideo.getTeacherId()));
                    if (Objects.isNull(courseComplete)) { //课程完成信息新建，提交视频教学记录
                        courseComplete = new PxTrainingCourseComplete();
                        //设置未完成
                        courseComplete.setStatus(Constants.COMMON_DELETE);
                        courseComplete.setTrainingCourseId(courseVideo.getTrainingCourseId());
                        courseComplete.setTrainingId(pxTrainingCourse.getTrainingId());
                        //学分
                        courseComplete.setTeacherScore(pxTrainingCourse.getClassHour());
                        courseComplete.setTeacherId(courseVideo.getTeacherId());
                        //已提交视频教学
                        courseComplete.setIsVideo(Constants.COMMON_SUCCESS);
                        trainingCourseCompleteService.save(courseComplete);
                    } else {
                        if (courseComplete.getStatus() == Constants.COMMON_DELETE) { //判断课程是否完成
                            //课程未签到
                            if (courseComplete.getIsSignin() == Constants.COMMON_DELETE) {
                                boolean isScore=false;
                                //课后作业和问卷调查
                                if (pxTrainingCourse.getIsWork()==Constants.COMMON_SUCCESS && pxTrainingCourse.getIsQuestionnaire() ==Constants.COMMON_SUCCESS){
                                    //课后作业和问卷调查已完成
                                    if (courseComplete.getIsSubWork()==Constants.COMMON_SUCCESS&&courseComplete.getIsSubQuestionnaire()==Constants.COMMON_SUCCESS){
                                        courseComplete.setStatus(Constants.COMMON_SUCCESS);
                                        //todo 加分
                                        isScore=true;
                                    }
                                }else if(pxTrainingCourse.getIsWork()==Constants.COMMON_SUCCESS &&  pxTrainingCourse.getIsQuestionnaire() ==Constants.COMMON_DELETE) { //没有问卷
                                    //课后作业已完成
                                    if (courseComplete.getIsSubWork()==Constants.COMMON_SUCCESS){
                                        courseComplete.setStatus(Constants.COMMON_SUCCESS);
                                        //todo 加分
                                        isScore=true;
                                    }
                                }else if(pxTrainingCourse.getIsWork()==Constants.COMMON_DELETE &&  pxTrainingCourse.getIsQuestionnaire() ==Constants.COMMON_DELETE) { //没有课后作业
                                    //问卷已完成
                                    if (courseComplete.getIsSubQuestionnaire()==Constants.COMMON_SUCCESS){
                                        courseComplete.setStatus(Constants.COMMON_SUCCESS);
                                        //todo 加分
                                        isScore=true;
                                    }
                                }else{ //没有课后作业和问卷
                                    courseComplete.setStatus(Constants.COMMON_SUCCESS);
                                    //todo 加分
                                    isScore=true;
                                }
                                //已提交视频
                                courseComplete.setIsVideo(Constants.COMMON_SUCCESS);
                                trainingCourseCompleteService.updateById(courseComplete);
                                //执行加分
                                if (isScore){
                                    trainingTeacherService.addScore(courseComplete.getTrainingId(),courseComplete.getTeacherId(),courseComplete.getTeacherScore());
                                }
                            }
                        }
                    }
                }

            });
        }
    }

    @Override
    public void deleteVideo(String videoId) {
        //查询视频是否有记录
        long count=trainingCourseVideoService.count(Wrappers.<PxTrainingCourseVideo>lambdaQuery().eq(PxTrainingCourseVideo::getVideoId,videoId));
        if (count>0){
            throw new GlobalException("视频已观看不能删除");
        }
        //删除视频
        trainingCourseFileService.removeById(videoId);
    }

    @Override
    public JSONObject getMyCourseInfo(String teacherId) throws ExecutionException, InterruptedException {
        //统计我的未读信息
        CompletableFuture<Long> msgNumber=CompletableFuture.supplyAsync(()-> trainingMessageService.count(Wrappers.<PxTrainingMessage>lambdaQuery().eq(PxTrainingMessage::getTeacherId, teacherId)
                .eq(PxTrainingMessage::getStatus,Constants.COMMON_DELETE)));
        //统计我的学分
        CompletableFuture<Integer> socre=CompletableFuture.supplyAsync(()-> trainingTeacherService.getMyScore(teacherId));
        //统计我的待提交课程
        Integer noWorknumber=trainingCourseHomeworkService.getTrainingWork(teacherId, 0).size();
        //等待线程处理
        CompletableFuture.allOf(socre,msgNumber).join();
        JSONObject resJson=new JSONObject();
        resJson.put("msgNumber",msgNumber.get());
        resJson.put("socre",socre.get());
        resJson.put("noWorknumber",noWorknumber);
        return resJson;
    }


}
